 package com.t.bricks.bussiness.service.wares.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.common.CommonDao;
import com.t.bricks.bussiness.db.entity.wares.Wares;
import com.t.bricks.bussiness.service.wares.WaresService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.data.MoneyUtil;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 商品信息'Wares表'基本业务操作类接口实现
 * @author HuangXinBian
 * @DateTime 2022年07月19日 15:26:23
 */
@Service
public class WaresServiceImpl implements WaresService<Wares> {
	
	@Autowired
	private CommonDao commonDao;

	/**
	 * 自定义新增方法
	 * @param wares
	 * @return
	 */
	@Override
	public MsgEmity addCustom(Wares wares) {
		MsgEmity me = ModuleUtil.objToEntity(wares, Wares.class);//转换数据,因为如果传递的是String,则可能是进行了跨包访问,要先用Json转换成实体对象
		if (!me.isSuccess()) {
			return me;
		}
		
		Wares waresData = me.getData();
		
		me = ModuleUtil.validDbEntityByAdd(waresData);// 通用的基本数据验证,如果要进行比较复杂的数据验证则可以屏蔽掉此方法自行编写
		if (!me.isSuccess()) {
			return me;
		}
		
		if (null == waresData.getiMinBuy() || waresData.getiMinBuy() < 1) {
			waresData.setiMinBuy(1);
		}
		
		if (null == waresData.getiMaxBuy() || waresData.getiMaxBuy() < 1) {
			waresData.setiMaxBuy(9999999);
		}
		
		if (null == waresData.getmReferencePrice() || !MoneyUtil.isPlus(waresData.getmReferencePrice())) {
			return MsgEmity.err(8021, "参考价格必须是正数");
		}
		
		if (null == waresData.getmCurrentPrice() || !MoneyUtil.isPlus(waresData.getmCurrentPrice())) {
			return MsgEmity.err(8022, "当前价格必须是正数");
		}
		
		if (null == waresData.getmCouponPayMin() || !MoneyUtil.isPlus(waresData.getmCouponPayMin())) {
			return MsgEmity.err(8023, "优惠卷最小金额必须是正数");
		}
		
		if (null == waresData.getmCouponPayMax() || !MoneyUtil.isPlus(waresData.getmCouponPayMax())) {
			return MsgEmity.err(8024, "优惠卷最大金额必须是正数");
		}
		
		if (null == waresData.getiTallyPay() || waresData.getiTallyPay().intValue() < 1) {
			waresData.setiTallyPay(0);
		}
		
		if (null == waresData.getiTallyLargess() || waresData.getiTallyLargess().intValue() < 1) {
			waresData.setiTallyLargess(0);
		}
		
		if (null == waresData.getiExpiryTime() || waresData.getiExpiryTime().intValue() < 1) {
			waresData.setiExpiryTime(999999);
		}
		
		if (null == waresData.getiExpiryTimeMonth() || waresData.getiExpiryTimeMonth().intValue() < 1) {
			waresData.setiExpiryTime(240);
		}
		
		if (null == waresData.getiExpiryTime() || waresData.getiExpiryTime().intValue() < 1) {
			waresData.setiExpiryTime(29);
		}
		
		if (null == waresData.getiExpiryTime() || waresData.getiExpiryTime().intValue() < 1) {
			waresData.setiExpiryTime(23);
		}
		
		if (null == waresData.getiWarnTime() || waresData.getiWarnTime().intValue() < 1) {
			waresData.setiWarnTime(999999);
		}
		
		if (null == waresData.getiLockDuration() || waresData.getiLockDuration().intValue() < 1) {
			waresData.setiLockDuration(999999);
		}
		
		me = this.validByAdd(waresData);
		if (!me.isSuccess()) {
			return me;
		}
		
		waresData.setsId(null);//清空记录编号
		
		me = ModuleUtil.supplyDbEntityAttrByAdd(waresData);// 补充数据,如添加id、当前用户、当前时间等
		if (!me.isSuccess()) {
			return me;
		}
		
		return commonDao.add(waresData);
	}

	/**
	 * 自定义修改方法
	 * @param wares
	 * @return
	 */
	@Override
	public MsgEmity editCustom(Wares wares) {
		MsgEmity me = ModuleUtil.objToEntity(wares, Wares.class);//转换数据,因为如果传递的是String,则可能是进行了跨包访问,要先用Json转换成实体对象
		if (!me.isSuccess()) {
			return me;
		}
		
		Wares waresData = me.getData();
		
		me = ModuleUtil.validBaseDataByEdit(waresData);// 通用的基本数据验证(编辑),如果要进行比较复杂的数据验证则可以屏蔽掉此方法自行编写
		if (!me.isSuccess()) {
			return me;
		}
		
		if (null != waresData.getiMinBuy() && waresData.getiMinBuy() < 1) {
			return MsgEmity.err(8021, "最小购买量必须是正数");
		}
		
		if (null != waresData.getiMaxBuy() && waresData.getiMaxBuy() < 1) {
			return MsgEmity.err(8022, "最大购买量必须是正数");
		}
		
		if (null != waresData.getmReferencePrice() && !MoneyUtil.isPlus(waresData.getmReferencePrice())) {
			return MsgEmity.err(8023, "参考价格必须是正数");
		}
		
		if (null != waresData.getmCurrentPrice() && !MoneyUtil.isPlus(waresData.getmCurrentPrice())) {
			return MsgEmity.err(8024, "当前价格必须是正数");
		}
		
		if (null != waresData.getmCouponPayMin() && !MoneyUtil.isPlus(waresData.getmCouponPayMin())) {
			return MsgEmity.err(8025, "优惠卷最小金额必须是正数");
		}
		
		if (null != waresData.getmCouponPayMax() && !MoneyUtil.isPlus(waresData.getmCouponPayMax())) {
			return MsgEmity.err(8026, "优惠卷最大金额必须是正数");
		}
		
		if (null != waresData.getiTallyPay() && waresData.getiTallyPay().intValue() < 1) {
			return MsgEmity.err(8027, "支付积分必须是正数");
		}
		
		if (null != waresData.getiTallyLargess() && waresData.getiTallyLargess().intValue() < 1) {
			return MsgEmity.err(8028, "赠送积分不能是负数");
		}
		
		if (null != waresData.getiExpiryTime() && waresData.getiExpiryTime().intValue() < 1) {
			return MsgEmity.err(8029, "有效期(单位小时)不能是负数");
		}
		
		if (null != waresData.getiExpiryTimeMonth() && waresData.getiExpiryTimeMonth().intValue() < 1) {
			return MsgEmity.err(8029, "有效期-月不能是负数");
		}
		
		if (null != waresData.getiExpiryTimeDay() && waresData.getiExpiryTimeDay().intValue() < 1) {
			return MsgEmity.err(8030, "有效期-天不能是负数");
		}
		
		if (null != waresData.getiExpiryTime() && waresData.getiExpiryTime().intValue() < 1) {
			return MsgEmity.err(8031, "有效期-时不能是负数");
		}
		
		if (null != waresData.getiWarnTime() && waresData.getiWarnTime().intValue() < 1) {
			return MsgEmity.err(8032, "过期前预警时间(小时)不能是负数");
		}
		
		if (null != waresData.getiLockDuration() && waresData.getiLockDuration().intValue() < 1) {
			return MsgEmity.err(8032, "锁定时长(单位:分)不能是负数");
		}
		
		me = this.validByEdit(waresData);
		if (!me.isSuccess()) {
			return me;
		}
		
		ModuleUtil.supplyDbEntityAttrByEdit(waresData);// 补充数据
		
		//--提交数据保存--//
		return commonDao.edit(waresData, false);//注意:更新成功后对象属性iVersion值将被+1
	}

	/**
	 * 取分类名称
	 * @return MsgEmity 返回查询结果
	 */
	@Override
	public MsgEmity getTypeName() {
		MsgEmity me = this.callMapping("groupTypeName");
		if (!me.isSuccess()) {
			return me;
		}
		
		List<String> list = me.getData();
		Map<String, List<String>> map = new HashMap<>(list.size());
		
		for (String string : list) {
			if (null == string || "".equals(string)) {
				continue;
			}
			
			String[] tmp = string.trim().split("\\|");
			if (tmp.length < 1) {
				continue;
			}
			
			List<String> value = map.get(tmp[0]);
			if (null == value) {
				value = new ArrayList<String>();
			}
			
			if (tmp.length < 2) {
				map.put(tmp[0], value);
				continue;
			}
			
			value.add(tmp[1]);
			map.put(tmp[0], value);
		}
		
		return MsgEmity.success(map, "查询成功");
	}

	/**
	 * 取商品列表
	 * @param sType 分类
	 * @return MsgEmity 返回查询结果
	 */
	@Override
	public MsgEmity findList(String sType) {
		if (null == sType || "".equals(sType.trim())) {
			sType = null;
		} else {
			sType = sType.trim();
		}
		
		return this.callMapping("findList", sType);
	}

}
